<?
 
 class Controller_Registration
  extends Controller_Base{

 	function index($args) {
		$template = new Template('registration.tmpl'); 

		$user = new Model_User();
		
		if($user->is_logged()) 
			return array(
				status 		=> 302,
				location	=> '/'
			);

 		$template->setArgs(array(
 			do_not_show_login_form	=> 1,
 			show_form				=> 1,
 			menu					=> $this->menu->getMenu(1),
 		));
		
		if(isset($_GET[ok])) {
			$template->setArg(show_form, 0);
			$template->setArg(show_success_message,1);
		}

		if($_POST['action'] === 'add_user') {

	 		$sigs = array(
	 			'new_user_name'	=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
	 			'password'		=> array('type' => 'string','function'=>array(),'required' =>1),
	 			'password2'		=> array('type' => 'string','function'=>array(),'required' =>1),
				'email'			=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
				'real_name'		=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
				'phone'			=> array('type' => 'string','function'=>array()),
			);			
				
			sanitize_vars($_POST,$sigs,null);		
			
			try{
				
				//try to add user
				$confirmation_id = $user->add($_POST); 
			// catching exeptions 
			} catch( UserException $e ) {
				$error = $e->getMessage();
				
			} 
	
			
			if($confirmation_id) {
//				$mail = new Model_Mail();
//
//				//TODO make correct email message
//				
//				$text = "Для завершения регистрации перейдите по <a href=".hostname."/confirmation?action=activate&code=$confirmation_id>ссылке</a>";
//
//				$mail->send($_POST['email'],'Подтверждение регистрации',$text);
//				
				return array(status 	=> 302,	location 	=> "/registration?ok");
			}
//$this->registry['debug']->warn($error);
			$template->setArgs(
				array(
					registration_error		=> $error,
					new_user_name 			=> $_POST[new_user_name],
					email	  				=> $_POST[email],
					real_name				=> $_POST[real_name],
					phone					=> $_POST[phone],
				)
			);
		
		}
 	
 		return array(
 			status 		=> 0,
 			template 	=> $template 
 		);	
 	}

 }
 
?>